package conf

import (
	"fmt"
	"os"
	"path/filepath"
	"svr/src/logs"
	"svr/src/msg"
	"svr/src/xutil"
	"sync"
	"time"

	// "github.com/golang/protobuf/proto"
	"google.golang.org/protobuf/proto"
)

func testpb() {
	// MsgGao
	// msg := &MsgGao.GetPlayerR{}
	// msg.GetPlayerR{}
	// fmt.Println("Aaaa111222", msg)
	p := &msg.Player{
		Id:   9,
		Name: "a",
	}
	msgR := &msg.GetPlayerA{
		P: p,
	}
	bs, _ := proto.Marshal(msgR)
	fmt.Println("Aa2222", bs)

	// m := &msg.GetPlayerA{}
	// proto.Unmarshal(bs, m)
	// fmt.Println("Aa333", m)

	// var m = jsonpb.Marshaler{
	// 	OrigName:     true,
	// 	EnumsAsInts:  true,
	// 	EmitDefaults: true,
	// }
	// s, _ := m.MarshalToString(msgR)
	// path := string(proto.MessageReflect(msgR).Descriptor().FullName().Name()) //获取消息名
	path := proto.MessageName(msgR)
	bs, _ = proto.Marshal(msgR)
	fmt.Println("----------", path, bs) //压缩消息
}

func testCh() {
	ch := make(chan string)
	// go func() {
	// 	logs.Debugf("11")
	// 	time.Sleep(time.Second)
	// 	logs.Debugf("22")
	// 	ch <- "aaaa"
	// 	ch <- "cccc"
	// 	time.Sleep(time.Second)
	// 	logs.Debugf("2222")
	// 	ch <- "dd"
	// 	ch <- "sd"
	// 	ch <- "df"
	// 	time.Sleep(time.Second)
	// 	logs.Debugf("33")
	// 	ch <- "bbb"
	// 	time.Sleep(time.Second)
	// }()
	go func() {
		ch <- "aaaa"
		ch <- "bbbb"
		ch <- "cccc"
		ch <- "dddd"
	}()
	go func() {
		ch <- "1111"
		ch <- "2222"
		ch <- "3333"
		ch <- "4444"
	}()
	// go func() {
	for {
		m := <-ch
		logs.Debugf("m:", m)
		// time.Sleep(time.Second)
	}
	// }()
	logs.Debugf("sssss")
}

func testCh2() {
	ch := make(chan string)
	go func() {
		fmt.Println("---------111-------")
		time.Sleep(time.Second * 3)
		fmt.Println("---------222-------")
		ch <- "aaaaaa"
		time.Sleep(time.Second)
		fmt.Println("---------333-------")
	}()
	m := <-ch
	logs.Debugf("m:", m)
}

func testIo(bs []byte) {
	// r := bytes.NewReader(bs)
	// var num int32
	// err := binary.Read(r, binary.BigEndian, &num) // 或者使用binary.LittleEndian
	// return num, err
}

func worker(id int, wg *sync.WaitGroup) {
	defer wg.Done() // 确保在函数结束时调用Done

	fmt.Printf("Worker %d starting\n", id)

	time.Sleep(time.Second) // 模拟工作
	fmt.Printf("Worker %d done\n", id)
}

func testWork() {
	var wg sync.WaitGroup

	for i := 1; i <= 5; i++ {
		wg.Add(1) // 增加等待的goroutine数量
	}

	for i := 0; i < 9; i++ {
		go worker(i, &wg)
	}

	wg.Wait() // 等待所有worker完成
	fmt.Println("All workers done")
}

func testPath() {
	// 获取当前执行文件的绝对路径
	exePath, err := os.Executable()
	if err != nil {
		fmt.Println("Error getting executable path:", err)
		return
	}

	// 获取当前执行文件所在的目录
	exeDir := filepath.Dir(exePath)

	fmt.Println("Current executable directory:", exeDir, exePath)
}
func init() {

}
func Test() {
	fmt.Println("------------test 3 -----------------")
	runBolin()
	// su()
	// p := &msg.Player{
	// 	Name: "a",
	// 	Id:   9,
	// }
	// bs, _ := proto.Marshal(p)
	// logs.Debugf("Bs  ", bs)
	// user := db.User{Name: "Aaa"}
	// testpb()
}

func su() {
	var vals = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	lenVals := len(vals)
	head := "A101"
	mapCode := make(map[string]bool)
	for {
		code := head
		for i := 0; i < 12; i++ {
			code += string(vals[xutil.GetRandomInt(0, lenVals)])
		}
		_, ok := mapCode[code]
		if !ok {
			logs.WriteDirect(code)
			mapCode[code] = true
			ln := len(mapCode)
			if ln%100 == 0 {
				fmt.Println("count :", ln)
			}
			if ln == 10000 {
				return
			}
		}
	}

}
